package com.XinYun.Library.modules.admin.bookCRUD.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.XinYun.Library.modules.admin.bookCRUD.service.BookService;
import com.XinYun.Library.modules.admin.common.JSON.AdminJSON;
import com.XinYun.Library.modules.commons.BasicJson;
import com.XinYun.Library.modules.commons.BasicServlet;
import com.XinYun.Library.utils.HTTPRequestUtils.HTTPRequestUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/api/book/*")
public class BookServlet extends BasicServlet{
    private BookService bookService = new BookService();

    /**
     * 分发方法
     * @param req
     * @param resp
     * @param methodName
     * @throws IOException
     * @throws NoSuchMethodException
     * @throws ServletException
     */
    @SuppressWarnings("AlibabaSwitchStatement")
    private void bookCRUDDistributor(HttpServletRequest req, HttpServletResponse resp, String methodName) throws IOException, NoSuchMethodException, ServletException {
        //设置返回JSON
        AdminJSON json = new AdminJSON();
        JSONObject jsonObj = null;
        //解析接收JSON
        //获取请求方式
        if(!"GET".equals(req.getMethod())){
            jsonObj = JSONUtil.parseObj(HTTPRequestUtil.getBody(req));
        }
        //方法分配
        switch (methodName){
            case "stateCnt"->bookService.getBookStateCount(json);
            case "addBook"-> bookService.addBook(json, jsonObj);
            case "alterBook"-> bookService.alertBookInfo(json,jsonObj);
            case "deleteBook"-> bookService.deleteBook(json,jsonObj);
            case "getBookClassInfo"-> bookService.getBookClassInfo(json,jsonObj);
            default -> throw new NoSuchMethodException("没有该方法");
        }
        //发送
        sendJson(json,resp);
    }

    /**
     * 书籍状态统计
     */
    public void stateCnt(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, NoSuchMethodException {
        bookCRUDDistributor(req,resp,"stateCnt");
    }

    /**
     * 添加一类书
     */
    public void addBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, NoSuchMethodException {
        bookCRUDDistributor(req,resp,"addBook");
    }

    /**
     * 需要改书籍信息
     */
    public void alterBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, NoSuchMethodException {
        bookCRUDDistributor(req,resp,"alterBook");
    }

    /**
     * 删除书籍
     */
    public void deleteBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, NoSuchMethodException {
        bookCRUDDistributor(req,resp,"deleteBook");
    }

    /**
     * 获得书的信息
     */
    public void getBookClassInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, NoSuchMethodException {
        bookCRUDDistributor(req,resp,"getBookClassInfo");
    }

    /**
     * 获得书的信息
     */
    public void getBookClassInfoNum(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, NoSuchMethodException {
        bookCRUDDistributor(req,resp,"getBookClassInfoNum");
    }
}
